package com.algorithm;

/**
 * @author: sun.hongliang
 * @create: 2019/12/12 11:55
 */
public class MyPowTest {
    public static void main(String[] args) {

    }

    public double myPow(double x, int n) {
        if (n == 0) return 1;
        if (n < 0) {
            n = -n;
            x = 1 / x;
        }
        return fastPow(x, n);
    }

    private double fastPow(double x, int n) {
        if (n == 0) return 1;
        double half = fastPow(x, n / 2);
        if (n % 2 == 0) {
            return half * half;
        } else {
            return half * half * x;
        }
    }
}
